
/* Generated by Interface Builder */

#import "Dispatcher.h"
#import "LPCPlot.h"
#import "LPCreate.h"
#import "PCHPlot.h"
#import "LPCView.h"
#import "Help.h"
#import <appkit/OpenPanel.h>
#import <appkit/SavePanel.h>
#import <appkit/Application.h>
#import <strings.h>
#import <stdio.h>
#import <libc.h>
#import <appkit/Control.h>
#import <appkit/Window.h>
#import <defaults/defaults.h>

@implementation Dispatcher
+ new
{
	self = [super new];
	return self;
}

- init
{
	char *suffix;
    
	strcpy(appDir, NXArgv[0]);
	if (suffix = rindex(appDir,'/')) 
		*suffix  = '\0'; 	/* remove executable name */
	if (appDir) 
		chdir(appDir); 
	getwd(appDir);
	selection = NULL;
	[prefPanel setDelegate:self];
	[self setPrefs];
	return self;
}

- (const char *)appDirectory;
/* Returns the path to the app wrapper (for Help object).
 */
{
    return appDir;
}

- setPrefs
{
	const char *owner;
	char *temp;
	
	owner = "lpcview";
	temp = (char *)NXReadDefault(owner, "lpc");
	if (temp)
		[deflpc setStringValue:(char *)NXReadDefault(owner, "lpc")];
	
	temp = (char *)NXReadDefault(owner, "stabilize");
	if (temp)
		[defstabilize setStringValue:(char *)NXReadDefault(owner, "stabilize")];
	
	temp = (char *)NXReadDefault(owner, "ptrack");
	if (temp)
		[defptrack setStringValue:(char *)NXReadDefault(owner, "ptrack")];
	
	temp = (char *)NXReadDefault(owner, "merge");
	if (temp)
		[defmerge setStringValue:(char *)NXReadDefault(owner, "merge")];
	
	temp = (char *)NXReadDefault(owner, "hz");
	if (temp)
		[defhz setStringValue:(char *)NXReadDefault(owner, "hz")];
	
	temp = (char *)NXReadDefault(owner, "playpitch");
	if (temp)
		[defplaypitch setStringValue:(char *)NXReadDefault(owner, "playpitch")];
	
	temp = (char *)NXReadDefault(owner, "poles");
	if (temp)
		[defpoles setIntValue:(int)atoi(temp)];
	
	temp = (char *)NXReadDefault(owner, "frames");
	if (temp)
		[defframes setIntValue:(int)atoi(temp)];
	
	temp = (char *)NXReadDefault(owner, "lowcps");
	if (temp)
		[deflowcps setFloatValue:(float)atof(temp)];
	
	temp = (char *)NXReadDefault(owner, "highcps");
	if (temp)
		[defhighcps setFloatValue:(float)atof(temp)];

	temp = (char *)NXReadDefault(owner, "pitchframe");
	if (temp)
		[defpitchframe setIntValue:(int)atof(temp)];

	return self;
}

- changePrefs:sender
{
	const	char *owner;
	
	owner = "lpcview";
	
	NXWriteDefault(owner, "lpc", [deflpc stringValue]);
	NXWriteDefault(owner, "stabilize", [defstabilize stringValue]);
	NXWriteDefault(owner, "ptrack", [defptrack stringValue]);
	NXWriteDefault(owner, "merge", [defmerge stringValue]);
	NXWriteDefault(owner, "hz", [defhz stringValue]);
	NXWriteDefault(owner, "playpitch", [defplaypitch stringValue]);
	NXWriteDefault(owner, "poles", [defpoles stringValue]);
	NXWriteDefault(owner, "frames", [defframes stringValue]);
	NXWriteDefault(owner, "lowcps", [deflowcps stringValue]);
	NXWriteDefault(owner, "highcps", [defhighcps stringValue]);
	NXWriteDefault(owner, "pitchframe", [defpitchframe stringValue]);
	return self;
}

- windowWillClose:sender
{
	const	char *owner;
	
	owner = "lpcview";

	NXWriteDefault(owner, "lpc", [deflpc stringValue]);
	NXWriteDefault(owner, "stabilize", [defstabilize stringValue]);
	NXWriteDefault(owner, "ptrack", [defptrack stringValue]);
	NXWriteDefault(owner, "merge", [defmerge stringValue]);
	NXWriteDefault(owner, "hz", [defhz stringValue]);
	NXWriteDefault(owner, "playpitch", [defplaypitch stringValue]);
	NXWriteDefault(owner, "poles", [defpoles stringValue]);
	NXWriteDefault(owner, "frames", [defframes stringValue]);
	NXWriteDefault(owner, "lowcps", [deflowcps stringValue]);
	NXWriteDefault(owner, "highcps", [defhighcps stringValue]);
	NXWriteDefault(owner, "pitchframe", [defpitchframe stringValue]);

	return self;
}

- createPlot:sender
{
	[LPCPlot new:"Untitled"];
	return self;
}

- createPCH:sender
{
	[PCHPlot new:"Untitled"];
	return self;
}

- create:sender
{
	[LPCreate new];
	return self;
}

- viewFile:(char *)filename
{
	[LPCPlot new:filename];
	return self;
}

- viewPCHfile:(char *)filename
{
	[PCHPlot new:filename];
	return self;
}

- openLPCfile:sender
{
	char	*types[2];
	char	file[1024];
	static id	openPan = nil;
	
	types[0] = "lpc";
	types[1] = 0;
	if (!openPan)
		openPan = [OpenPanel new];
	if ([openPan runModalForTypes:types]) {
		strcpy(file, [openPan filename]);
		[LPCPlot new:file];
	}
	return self;
}

- openPCHfile:sender
{
	char *pchtypes[2];
	char file[1024];
	static id openPan = nil;
		
	pchtypes[0] = "pch";
	pchtypes[1] = 0;	
	if (!openPan)
		openPan = [OpenPanel new];
	if ([openPan runModalForTypes:pchtypes]) {
		strcpy(file, [openPan filename]);
		[PCHPlot new:file];
	}
	return self;

}

- cut:sender
{
	if (!selection) {
		selection = NXOpenMemory(NULL, 0, NX_READWRITE);
	}
	else {
		NXCloseMemory(selection, NX_FREEBUFFER);
		selection = NXOpenMemory(NULL, 0, NX_READWRITE);
	}
	[[[NXApp keyWindow] delegate] doCut:selection];
	return self;
}

- copy:sender
{
	// get a new memory stream to copy into
	if (!selection) {
		selection = NXOpenMemory(NULL, 0, NX_READWRITE);
	}	
	else {
		NXCloseMemory(selection, NX_FREEBUFFER);
		selection = NXOpenMemory(NULL, 0, NX_READWRITE);
	}
	[[[NXApp keyWindow] delegate] doCopy:selection];
	return self; 
}

- insert:sender
{
	float	*data;
	int	len, maxlen;
	
	if (!selection) 
		return self;
	NXGetMemoryBuffer(selection, (char **)&data, &len, &maxlen);
	[[[NXApp keyWindow] delegate] doInsert:data:len];
	return self;
}

- (BOOL)appAcceptsAnotherFile:sender
{
	return YES;
}

- (char *)lpc
{
	return (char *)[deflpc stringValue];
}

- (char *)stabilize
{
	return (char *)[defstabilize stringValue];
}

- (char *)ptrack
{
	return (char *)[defptrack stringValue];
}

- (char *)merge
{
	return (char *)[defmerge stringValue];
}

- (char *)hz
{
	return (char *)[defhz stringValue];
}

- (char *)playpitch
{
	return (char *)[defplaypitch stringValue];
}

- (int)poles
{
	return [defpoles intValue];;
}

- (int)frames
{
	return [defframes intValue];
}

- (float)lowcps
{
	return [deflowcps floatValue]; 
}

- (float)highcps
{
	return [defhighcps floatValue];
}

- (int)ptchframe
{
	return [defpitchframe intValue];
}

@end
